#include "ChessManMoveTable.h"

ChessManMoveTable::ChessManMoveTable()
{
	InitChessManMoveTable();
}

ChessManMoveTable::~ChessManMoveTable()
{

}

void ChessManMoveTable::InitChessManMoveTable()
{
	InitPawnMoveTable();
	InitRookMoveTable();
	InitKnightMoveTable();
	InitBishopMoveTable();
	InitQueenMoveTable();
	InitKingMoveTable();
}

void ChessManMoveTable::InitPawnMoveTable()
{
	ChessManMoveDir dir(1,0,1);
	m_lstChessManMoveTab[Pawn].push_back(dir);

	//the pawn in it's origin position can move forward 2 step
	dir.iMaxStep = 2;
	m_lstChessManMoveTab[Pawn].push_back(dir);

	//the pawn capture direction
	//(1,1) direction
	dir.y = 1;
	dir.iMaxStep = 1;
	m_lstChessManMoveTab[Pawn].push_back(dir);

	//(1,-1) direction
	dir.y = -1;
	m_lstChessManMoveTab[Pawn].push_back(dir);
}

void ChessManMoveTable::InitRookMoveTable()
{
	//(0,1) direction
	ChessManMoveDir dir(0,1,8);
	m_lstChessManMoveTab[Rook].push_back(dir);
	
	//(0,-1) direction
	dir.y = -1;
	m_lstChessManMoveTab[Rook].push_back(dir);

	//(1,0) direction
	dir.x = 1;
	dir.y = 0;
	m_lstChessManMoveTab[Rook].push_back(dir);

	//(-1,0) direction
	dir.x = -1;
	m_lstChessManMoveTab[Rook].push_back(dir);
}

void ChessManMoveTable::InitKnightMoveTable()
{
	//(-1,2) direction
	ChessManMoveDir dir(-1,2,1);
	m_lstChessManMoveTab[Knight].push_back(dir);

	//(1,2) direction
	dir.x = 1;
	m_lstChessManMoveTab[Knight].push_back(dir);

	//(1,-2) direction
	dir.y = -2;
	m_lstChessManMoveTab[Knight].push_back(dir);

	//(-1,-2) direction
	dir.x = -1;
	m_lstChessManMoveTab[Knight].push_back(dir);

	//(-2,1) direction
	dir.x = -2;
	dir.y = 1;
	m_lstChessManMoveTab[Knight].push_back(dir);

	//(-2,-1) direction
	dir.y = -1;
	m_lstChessManMoveTab[Knight].push_back(dir);

	//(2,-1) direction
	dir.x = 2;
	m_lstChessManMoveTab[Knight].push_back(dir);

	//(2,1) direction
	dir.y = 1;
	m_lstChessManMoveTab[Knight].push_back(dir);
}

void ChessManMoveTable::InitBishopMoveTable()
{
	//(1,1) direction
	ChessManMoveDir dir(1,1,8);
	m_lstChessManMoveTab[Bishop].push_back(dir);

	//(-1,1) direction
	dir.x = -1;
	m_lstChessManMoveTab[Bishop].push_back(dir);

	//(-1,-1) direction
	dir.y = -1;
	m_lstChessManMoveTab[Bishop].push_back(dir);

	//(1,-1) direction
	dir.x = 1;
	m_lstChessManMoveTab[Bishop].push_back(dir);
}

void ChessManMoveTable::InitQueenMoveTable()
{
	//(0,1) direction
	ChessManMoveDir dir(0,1,8);
	m_lstChessManMoveTab[Queen].push_back(dir);

	//(0,-1) direction
	dir.y = -1;
	m_lstChessManMoveTab[Queen].push_back(dir);

	//(1,0) direction
	dir.x = 1;
	dir.y = 0;
	m_lstChessManMoveTab[Queen].push_back(dir);

	//(-1,0) direction
	dir.x = -1;
	m_lstChessManMoveTab[Queen].push_back(dir);

	//(1,1) direction
	dir.x = 1;
	dir.y = 1;
	m_lstChessManMoveTab[Queen].push_back(dir);

	//(-1,1) direction
	dir.x = -1;
	m_lstChessManMoveTab[Queen].push_back(dir);

	//(-1,-1) direction
	dir.y = -1;
	m_lstChessManMoveTab[Queen].push_back(dir);

	//(1,-1) direction
	dir.x = 1;
	m_lstChessManMoveTab[Queen].push_back(dir);
}

void ChessManMoveTable::InitKingMoveTable()
{
	//(0,1) direction
	ChessManMoveDir dir(0,1,2);
	m_lstChessManMoveTab[King].push_back(dir);

	//(0,-1) direction
	dir.y = -1;
	m_lstChessManMoveTab[King].push_back(dir);

	//(1,0) direction
	dir.iMaxStep = 1;
	dir.x = 1;
	dir.y = 0;
	m_lstChessManMoveTab[King].push_back(dir);

	//(-1,0) direction
	dir.x = -1;
	m_lstChessManMoveTab[King].push_back(dir);

	//(1,1) direction
	dir.x = 1;
	dir.y = 1;
	m_lstChessManMoveTab[King].push_back(dir);

	//(-1,1) direction
	dir.x = -1;
	m_lstChessManMoveTab[King].push_back(dir);

	//(-1,-1) direction
	dir.y = -1;
	m_lstChessManMoveTab[King].push_back(dir);

	//(1,-1) direction
	dir.x = 1;
	m_lstChessManMoveTab[King].push_back(dir);
}

std::vector<ChessManMoveDir>* ChessManMoveTable::GetChessManMoveTab( int chessMan )
{
	return &m_lstChessManMoveTab[chessMan];
}

ChessManMoveTable* ChessManMoveTable::Instance()
{
	static ChessManMoveTable* pInstance = NULL;

	if(!pInstance)
		pInstance = new ChessManMoveTable();

	return pInstance;
}
